package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.kit.PoiKit.Pair;
import com.jfinal.ext.kit.PoiKit.SheetData;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.Material;
import com.zzmcc.jms.model.News;
import com.zzmcc.jms.model.Project;
import com.zzmcc.jms.model.Stock;
import util.ChristStringUtil;
import util.ModelUtils;

import java.util.*;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Material;
import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Stock;
import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Project;

public class StockController extends BaseController {

	public void out() {
		//订单入库清单
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("stock.name");
		String oid = getPara("stock.oid");
		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and b.name like ?");
			param.add("%" + name + "%");
		}

		if(ChristStringUtil.isNotEmpty(oid)){
			whee.append(" and a.oid = ?");
			param.add( oid);
		}

		Page<Stock> pager = Stock.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.oid,a.mid,a.price,a.quantity,a.createDate,a.memo,b.catalogue,b.name,b.unit,b.model,b.supplier ",
				" from " + TABLE_Stock +" a left join "+TABLE_Material+" b " +
						" on a.mid=b.id where a.type=1"+ whee.toString() + " order by createDate desc",
				param.toArray()
		) ;

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("oid",oid);
		render("../stock_list.html");
	}

	public void in() {
		//订单入库清单
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("stock.name");
		String oid = getPara("stock.oid");
		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and b.name like ?");
			param.add("%" + name + "%");
		}

		if(ChristStringUtil.isNotEmpty(oid)){
			whee.append(" and a.oid = ?");
			param.add( oid);
		}

		Page<Stock> pager = Stock.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.oid,a.mid,a.price,a.quantity,a.createDate,a.memo,b.catalogue,b.name,b.unit,b.model,b.supplier ",
				" from " + TABLE_Stock +" a left join "+TABLE_Material+" b " +
				" on a.mid=b.id where a.type=1"+ whee.toString() + " order by createDate desc",
				param.toArray()
				) ;

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("oid",oid);
		render("../stock_in_list.html");
	}

	@Before(AuthorizeInterceptor.class)
	public void add() {
		//入库操作
		render("../stock_add_input.html");
	}
	public void saveAdd() {
		Stock m = getModel(Stock.class, "stock");
		m.set("createDate",new Date());
		m.save();
		renderDWZSuccessJson("入库成功！");
	}

	public void index() {
		//物料出库
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String materialName = getPara("stock.materialName");
		String projectName = getPara("stock.projectName");
		String homeCounty = getPara("stock.homeCounty");
		String oid = getPara("stock.oid");
		String type = getPara("stock.type");

		if(ChristStringUtil.isNotEmpty(materialName)){
			whee.append(" and c.name like ?");
			param.add("%" + materialName + "%");
		}

		if(ChristStringUtil.isNotEmpty(projectName)){
			whee.append(" and b.name like ?");
			param.add("%" + projectName + "%");
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and b.homeCounty like ?");
			param.add("%" + homeCounty + "%");
		}

		if(ChristStringUtil.isNotEmpty(oid)){
			whee.append(" and a.oid = ?");
			param.add( oid);
		}

		if(ChristStringUtil.isNotEmpty(type)){
			whee.append(" and a.type = ?");
			param.add( type);
		}


		Page<Stock> pager = Stock.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.type,a.oid,a.mid,a.price,a.quantity,a.createDate,a.memo,\n" +
						"b.name projectName,b.homeCounty,c.catalogue,c.name,c.unit,c.model,c.supplier \n",
				" from " + TABLE_Stock +" a left join "+TABLE_Project+" b " +
				" on a.pid=b.pid \n" +
				"left join "+TABLE_Material +" c on a.mid=c.id \n"+
				"where 1=1"+ whee.toString(),
				param.toArray()
		) ;

		setAttr("pager", pager);
		setAttr("materialName", materialName);
		setAttr("projectName", projectName);
		setAttr("homeCounty", homeCounty);
		setAttr("oid", oid);
		setAttr("type", type);
		render("../stock_list.html");
	}

	@Before(AuthorizeInterceptor.class)
	public void sub(){
		//出库操作
		String oid =getPara("oid");
		String mid =getPara("mid");
		Stock m=Stock.dao.findFirst("SELECT sum(type*quantity)  maxNum from jms_stock a where oid=? and mid=?",oid,mid);
		Double maxNum=m.get("maxNum");
		setAttr("maxNum",maxNum);
		setAttr("oid",oid);
		setAttr("mid",mid);
		render("../stock_sub_input.html");
	}

	public void saveSub() {
		Stock m = getModel(Stock.class, "stock");
		//m.set("createDate",new Date());
		m.save();
		renderDWZSuccessJson("出库操作成功！");
	}


	public void lookupMaterial() {
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("name");
		String supplier = getPara("supplier");
		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(supplier)){
			whee.append(" and supplier like ?");
			param.add("%" + supplier + "%");
		}


		Page<Material> pager = Material.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select * ",
				" from " + TABLE_Material+"\n where 1=1"+ whee.toString(),
				param.toArray()
		) ;

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("supplier",supplier);
		render("../stock_lookup_material.html");
	}

	public void lookupProject() {
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("name");
		String homeCounty = getPara("homeCounty");
		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and homeCounty like ?");
			param.add("%" + homeCounty + "%");
		}


		Page<Project> pager = Project.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select * ",
				" from " + TABLE_Project+"\n where 1=1"+ whee.toString(),
				param.toArray()
		) ;

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("homeCounty",homeCounty);
		render("../stock_lookup_project.html");
	}


	public void lookupStock() {

		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("stock.name");
		String oid = getPara("stock.oid");
		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and b.name like ?");
			param.add("%" + name + "%");
		}

		if(ChristStringUtil.isNotEmpty(oid)){
			whee.append(" and a.oid = ?");
			param.add( oid);
		}

		Page<Stock> pager = Stock.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.oid,a.mid,a.price,a.quantity,a.createDate,a.memo,b.catalogue,b.name,b.unit,b.model,b.supplier ",
				" from " + TABLE_Stock +" a left join "+TABLE_Material+" b " +
						" on a.mid=b.id where a.type=1"+ whee.toString() + " order by createDate desc",
				param.toArray()
		) ;

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("oid",oid);

		render("../stock_lookup_stock.html");
	}

	@Before(AuthorizeInterceptor.class)
	public void delete() {
		String[] ids = getParaValues("ids");
		for(String id:ids){
			Stock.dao.deleteById(id);
		}
		renderDWZSuccessJson("删除成功!");
	}

	public void export(){

		String sql ="select a.id,(case when a.type=1 then '入库' else '出库' end) type,a.oid,a.mid,a.price,a.quantity,a.createDate,a.memo,\n" +
				"b.name projectName,b.homeCounty,c.catalogue,c.name,c.unit,c.model,c.supplier \n"+
				" from " + TABLE_Stock +" a left join "+TABLE_Project+" b " +
				" on a.pid=b.pid \n" +
				"left join "+TABLE_Material +" c on a.mid=c.id ";
		List<Record> stockDetail = Db.find(sql);

		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("stockDetail", stockDetail);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_stock.xls";
		String filename = "stock.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));


	}

}
